// pages/FanInfo/PersonInfo/Album/Album.js
const API = require('../../../../common/constant');
const util = require('../../../../utils/util');
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    photos: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // console.log(options.id);
    this.getUserInfo(options.id);
  },
  /**
   * 获取用户相册信息 
   */
  getUserInfo(id) {
    util.http({
      url: API.URL.USER.FIND_USERINFO,
      data: {
        userId: id
      },
      success: res => {
        console.log(res);
        this.changePhotoList(res.data.bussData.imageDTOList);
      },
      fail: err => {
        console.log(err);
      }
    })
  },
  /**
   * 预览照片
   */
  previewImage: function (e) {
    console.log(e);
    let urls = [];
    let photos = this.data.photos;
    for (let i = 0, iLen = photos.length; i < iLen; i++) {
      photos[i].imageurl.map(item => {
        urls.push(item)
      })
    }
    console.log(urls);
    wx.previewImage({
      urls: urls // 需要预览的图片http链接列表
    })
  },

  /**
   * 改照片的样式
   */
  changePhotoList: function (imageDTOList) {
    let photoData = [];
    for (let i in imageDTOList) {
      let month = parseInt(imageDTOList[i].imageTime.split('-')[1]);
      if (photoData.length == 0) {
        photoData.push({
          month: month,
          imageurl: []
        })
        photoData[0].imageurl.push(imageDTOList[i].fileUrl);
      } else {
        for (let key in photoData) {
          if (photoData[key].month == month) {
            photoData[key].imageurl.push(imageDTOList[i].fileUrl);
          } else {
            photoData.push({
              month: month,
              imageurl: []
            })
            photoData[photoData.length - 1].imageurl.push(imageDTOList[i].fileUrl);
          }
        }
      }
      this.setData({
        photos: photoData
      });
      console.log(this.data.photos);
      console.log(photoData);
    }
  },
})